package com.cy.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.study.common.BasePageReq;
import com.cy.study.entity.CommentEntity;
import com.cy.study.entity.ExperienceEntity;
import com.cy.study.entity.UserInfoEntity;
import com.cy.study.exception.BusinessException;
import com.cy.study.mapper.CommentMapper;
import com.cy.study.mapper.ExperienceMapper;
import com.cy.study.mapper.UserInfoMapper;
import com.cy.study.req.*;
import com.cy.study.res.*;
import com.cy.study.service.ExperienceService;
import com.cy.study.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Slf4j
@RequiredArgsConstructor
public class ExperienceServiceImpl extends ServiceImpl<ExperienceMapper, ExperienceEntity> implements ExperienceService {

    private final CommentMapper commentMapper;
    private final UserInfoMapper userInfoMapper;

    @Override
    public Page<UserCenterExperiencePageRes> userCenterExperiencePage(BasePageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Integer userId = SecurityUtils.getUserId();
        Page<UserCenterExperiencePageRes> res = this.getBaseMapper().
                userCenterExperiencePage(userId,page);
        return res;
    }

    @Override
    public Integer userCenterAddExperience(UserCenterAddExperienceReq req) {
        Integer userId = SecurityUtils.getUserId();
        ExperienceEntity experience = new ExperienceEntity();
        BeanUtils.copyProperties(req,experience);
        experience.setUserId(userId);
        experience.setCreateTime(LocalDateTime.now());
        experience.setUpdateTime(LocalDateTime.now());
        this.getBaseMapper().insert(experience);
        return experience.getId();
    }

    @Override
    public void userCenterUpdateExperience(UserCenterUpdateExperienceReq req) {
        ExperienceEntity experience = this.getBaseMapper().selectById(req.getId());
        if(experience==null){
            throw new BusinessException("心得id不存在，请重试");
        }
        if(req.getTitle()!=null&&req.getTitle()!=""){
            experience.setTitle(req.getTitle());
        }
        if(req.getBody()!=null&&req.getBody()!=""){
            experience.setBody(req.getBody());
        }
        experience.setUpdateTime(LocalDateTime.now());
        this.saveOrUpdate(experience);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userCenterDeleteExperience(UserCenterDeleteExperienceReq req) {
        //删心得
        ExperienceEntity experience = this.getBaseMapper().selectById(req.getId());
        if(experience==null){
            throw new BusinessException("心得id不存在，请重试");
        }
        this.removeById(experience);
        //删对应评论
        QueryWrapper<CommentEntity> commentWrapper = new QueryWrapper<>();
        commentWrapper.lambda().eq(CommentEntity::getExperienceId,req.getId());
        commentMapper.delete(commentWrapper);
    }

    @Override
    public Page<ExperiencePageRes> pageExperience(ExperiencePageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Page<ExperiencePageRes> res = this.getBaseMapper().pageExperience(req,page);
        return res;
    }

    @Override
    public void deleteExperience(ExperienceDeleteReq req) {
        boolean b = this.removeById(req.getId());
        if(!b){
            throw new BusinessException("心得不存在,请重试");
        }
    }

    @Override
    public ExperienceDetailRes detailExperience(Integer experienceId) {
        ExperienceEntity experience = this.getBaseMapper().selectById(experienceId);
        if(experience==null){
            throw new BusinessException("心得不存在，请重试");
        }
        ExperienceDetailRes res = new ExperienceDetailRes();
        BeanUtils.copyProperties(experience,res);
        UserInfoEntity user = userInfoMapper.selectById(experience.getUserId());
        if(user!=null){
            res.setUserName(user.getName());
        }
        return res;
    }

    @Override
    public Page<UserExperiencePageRes> userExperiencePageRes(UserExperienceReq req) {
        Page page = new Page(req.getPageNum(), req.getPageSize());
        Page<UserExperiencePageRes> res = this.getBaseMapper().userExperiencePageRes(req,page);
        return res;
    }

    @Override
    public UserExperienceDetailRes userExperienceDetail(Integer exId) {
        UserExperienceDetailRes res = new UserExperienceDetailRes();
        //先拿出心得详情
        ExperienceEntity experience = this.getBaseMapper().selectById(exId);
        if(experience==null){
            throw new BusinessException("心得为空,请确认心得id后重试");
        }
        res.setExperience(experience);
        Integer userId = experience.getUserId();
        UserInfoEntity userInfoEntity = userInfoMapper.selectById(userId);
        res.setUserName(userInfoEntity.getName());
        return res;

    }

}
